import numpy as np
import pandas as pd
import torch
from torch.utils.data import DataLoader
import matplotlib.pyplot as plt
from torch.utils.data import Dataset
import os
os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE"

class MyDataset(Dataset):
    # 定义数据集
    def __init__(self, filepath):
        # 从原始数据集中提取六个特征
        features = ["Pclass", "Sex", "Age", "SibSp", "Parch", "Fare"]
        data = pd.read_csv(filepath)
        self.len = data.shape[0]    # shape(多少行，多少列） shape[0]表示行数

        # data[features]的类型是DataFrame,先进行独热表示，然后转成array,最后转成tensor用于进行矩阵计算。
        self.x_data = torch.from_numpy(np.array(pd.get_dummies(data[features])))
        self.y_data = torch.from_numpy(np.array(data["Survived"]))

    def __getitem__(self, index):
        return self.x_data[index], self.y_data[index]

    def __len__(self):
        return self.len

# 建立数据集
dataset = MyDataset('train.csv')

# 建立数据集加载器
train_loader = DataLoader(dataset=dataset, batch_size=1, shuffle=True, num_workers=0)

# 定义模型
class Model(torch.nn.Module):
    def __init__(self):
        super(Model, self).__init__()
        self.linear1 = torch.nn.Linear(7,6)
        self.linear2 = torch.nn.Linear(6,3)
        self.linear3 = torch.nn.Linear(3,1)
        self.sigmoid = torch.nn.Sigmoid()

    def forward(self,x):
        x = self.sigmoid(self.linear1(x))
        x = self.sigmoid(self.linear2(x))
        x = self.sigmoid(self.linear3(x))
        return x

    # 定义的预测函数。
    def predict(self, x):
        with torch.no_grad():
            x = self.sigmoid(self.linear1(x))
            x = self.sigmoid(self.linear2(x))
            y = []
            for i in x:
                if i>0.5:
                    y.append(1)
                else:
                    y.append(0)
            return y

model = Model()

# 构造优化器和损失函数
criterion = torch.nn.BCEWithLogitsLoss(reduction="mean")
optimizer = torch.optim.SGD(model.parameters(), lr=0.05)

cost_list = []
if __name__ == '__main__':
    for epoch in range(1000):
        for i, data in enumerate(train_loader, 0):
            print(data)
            inputs, labels = data
            # 这里先转换了一下数据类型。
            inputs = inputs.float()
            labels = labels.float()

            y_pred = model(inputs)
            # 将维度压缩至1维。
            y_pred = y_pred.squeeze(-1)
            loss = criterion(y_pred, labels)
            cost_list.append(loss.item())
            print("epoch:",epoch, "cost:", loss.item())

            optimizer.zero_grad()
            loss.backward()
            optimizer.step()

test_data = pd.read_csv("test.csv")
features = ["Pclass", "Sex", "Age", "SibSp", "Parch", "Fare"]
test = torch.from_numpy(np.array(pd.get_dummies(test_data[features])))

# 进行预测
y = model.predict(test.float())

# 输出预测结果到文件
output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': y})
output.to_csv('my_predict.csv', index=False)